package weaver.jobs.cetc.duss.cron;

import weaver.common.DateUtil;
import weaver.conn.RecordSetDataSource;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.cetc.duss.utils.CetcUtil;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import static weaver.common.DateUtil.getDateTime;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-09-02
 * @Description: E8：根据浪潮查询视图获取数据，判断上次评价日期+评价周期-15天，是否等于当前日期，如果相等，则调用流程创建接口
 * @ModifyLog:
 **/

public class WeaHggftxCronJob extends BaseCronJob {
    @Override
    public void execute() {
        String currentDate = DateUtil.getCurrentDate();
        BaseBean baseBean = new BaseBean();
        CetcUtil cetcUtil = new CetcUtil();
        String formid = "567";
        RecordSetDataSource rds = new RecordSetDataSource("lc");
        rds.execute("select LSWLDW_WLDWBH,LSWLDW_DWMC,LSWLDW_CUSTOMFIELD85,LSWLDW_CUSTOMFIELD89,PJZQ,LSWLDW_CUSTOMFIELD65 from VW_CETE58_GYS" +
                " where (LSWLDW_CUSTOMFIELD85 is not null or PJZQ is not null)");
        while (rds.next()) {
            //供应商名称
            String LSWLDW_DWMC = Util.null2String(rds.getString("LSWLDW_DWMC"));
            //上次评价日期
            String LSWLDW_CUSTOMFIELD85 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD85"));
            String scpjrq = cetcUtil.formatDateToOA(LSWLDW_CUSTOMFIELD85);
            //采购组
            String LSWLDW_CUSTOMFIELD89 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD89"));
            //合格供方编号
            String LSWLDW_CUSTOMFIELD65 = Util.null2String(rds.getString("LSWLDW_CUSTOMFIELD65"));
            String userid = cetcUtil.getUserIdByName(LSWLDW_CUSTOMFIELD89);
            baseBean.writeLog(">>>>WeaHggftxCronJob>>>>>userid=" + userid);
            //评价周期
            int pjzq = Util.getIntValue(rds.getString("PJZQ"));
            Calendar cal = DateUtil.addYear(DateUtil.getCalendar(scpjrq), pjzq);
            DateUtil.addDay(cal, -15);
            String pjrq = DateUtil.getCalendarDate(cal);
            baseBean.writeLog(">>>>WeaHggftxCronJob>>>>>pjrq=" + pjrq + ",currentDate=" + currentDate);
            if (currentDate.equals(pjrq) && !userid.equals("")) {
                Map<String, String> wf = cetcUtil.getWorkflowId(formid);
                String workflowid = Util.null2String(wf.get("workflowid"));
                String requestname = Util.null2String(wf.get("workflowname")) + "-" + LSWLDW_CUSTOMFIELD89 + "-" + getDateTime();
                Map<String, String> requestMainMap = new HashMap<>();
                requestMainMap.put("sqr", userid);
                requestMainMap.put("gfmc", LSWLDW_DWMC);
                requestMainMap.put("scpjrq", scpjrq);
                requestMainMap.put("pjzq", pjzq + "");
                requestMainMap.put("hggfbh", LSWLDW_CUSTOMFIELD65);
               String newRequestid = cetcUtil.WorkflowCreateByRequestMap(userid, workflowid, requestname, "0", requestMainMap);
                baseBean.writeLog(">>>>WeaHggftxCronJob>>>>>newRequestid=" + newRequestid);

            }
        }

    }

}
